package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import objects.Board;
import objects.Player;
import utils.FactoryUI;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
/**
 * Servlet implementation class EndTurnServlet
 */
@WebServlet(name = "EndTurn", urlPatterns =
{
    "/EndTurn"
})
public class EndTurnServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EndTurnServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("application/json");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.flush();
		String playerOld = Board.getInstance().getCurrPlayerInstance().getPlayerName();
		Board.getInstance().advanceTurn();
		String playerNew = Board.getInstance().getCurrPlayerInstance().getPlayerName();
		
		JsonObject jsAction = new JsonObject();
		jsAction.addProperty("code", "endTurn");
		jsAction.addProperty("playerOld", playerOld);
		jsAction.addProperty("playerType", Board.getInstance().getCurrPlayerInstance().getType());
		jsAction.addProperty("playerNew", playerNew);
		jsAction.addProperty("message", "Player " +playerOld+ " finished his turn. It's " + playerNew + "'s turn to play.");
		Board.getInstance().addJsActions(jsAction);
		if (Board.getInstance().getCurrPlayerInstance().getType().equals("COMPUTER"))
		{
			ComputerPlayServlet.CompPlay();
		}
	}
	
	
	public static void turnEnder()
	{
		String playerOld = Board.getInstance().getCurrPlayerInstance().getPlayerName();
		Board.getInstance().advanceTurn();
		String playerNew = Board.getInstance().getCurrPlayerInstance().getPlayerName();
		JsonObject jsAction = new JsonObject();
		jsAction.addProperty("code", "endTurn");
		jsAction.addProperty("playerOld", playerOld);
		jsAction.addProperty("playerType", Board.getInstance().getCurrPlayerInstance().getType());
		jsAction.addProperty("playerNew", playerNew);
		jsAction.addProperty("message", "Player " +playerOld+ " finished his turn. It's " + playerNew + "'s turn to play.");
		Board.getInstance().addJsActions(jsAction);
		int nHumansInGame = Board.getInstance().getNumberOfHumansInGame();
		
		if (Board.getInstance().getCurrPlayerInstance().getType().equals("COMPUTER") && nHumansInGame > 0)
		{
			ComputerPlayServlet.CompPlay();
		}
	}
}
